﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Agatha.Common.InversionOfControl;
using Common.Logging;
using Source.Web.FrameWork;


namespace Source.Web
{
    public partial class Error : System.Web.UI.Page
    {
        private IConfigurationProvider configurationProvider;

        public Error()
        {
            configurationProvider = IoC.Container.Resolve<IConfigurationProvider>();
        }

        public string ErrorMessage { get; set; }

        public bool HasErrorMessage
        {
            get
            {
                return ErrorMessage != null;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            GetErrorMessage();
            DataBind();
        }

        public void GetErrorMessage()
        {
            Exception ex = Context.Items["Error"] as Exception;

            if(ex != null)
            {
                if(ShowErrorDetails())
                {
                    ErrorMessage = ex.ToString().Replace("\n","<br />");
                }
                else if (ex.InnerException != null)
                {
                    ErrorMessage = ex.InnerException.Message;
                }
                else
                {
                    ErrorMessage = ex.Message;
                }
            }
        }

        private bool ShowErrorDetails()
        {
            return configurationProvider.ShowErrorDetails;
        }
    }
}
